/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wekimini.gui;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import wekimini.util.Util;

/**
 *
 * @author rebecca
 */
public class GuiIONameCustomise extends javax.swing.JFrame {
    private int numNames;
    private String baseName;
    private String[] currentNames;
    private final List<IONameRow> rows = new ArrayList<>();
    private final NamesListReceiver namesListReceiver;

    int getNumNames() {
        return numNames;
    }

    void setNames(String[] names) {
        for (int i = 0; i < rows.size(); i++) {
            rows.get(i).setIOName(names[i]);
        }
    }
    public static enum IOType {INPUT, OUTPUT};
    private final IOType type;
    /**
     * Creates new form GUIInputNameCustomise
     */
    public GuiIONameCustomise() {
        initComponents();
        namesListReceiver = null;
        type = IOType.INPUT;
    }
    
    private void setPromptText() {
        if (type == IOType.INPUT) {
            this.setTitle("Enter Input Names");
            //labelPrompt.setText("Enter input names:");
        } else {
            this.setTitle("Enter Output Names");

           // labelPrompt.setText("Enter output names:");
        }
    }
    public GuiIONameCustomise(int numNames, String baseName, String[] currentNames, NamesListReceiver rec, IOType t) {
        initComponents();
        namesListReceiver = rec;
        this.numNames = numNames;
        this.baseName = baseName;
        this.type = t;
        setPromptText();
        if (currentNames == null) {
            currentNames = null;
        } else {
            this.currentNames = new String[currentNames.length];
            System.arraycopy(currentNames, 0, this.currentNames, 0, currentNames.length);
        }
        panelNamesList.removeAll();
        for (int i = 0; i < numNames; i++) {
            String thisName;
            if (currentNames != null && currentNames.length > i) {
                thisName = currentNames[i];
            } else {
                thisName = baseName + "-" + (i+1);
            }
            IONameRow r = new IONameRow((i+1), thisName);
            rows.add(r);
            panelNamesList.add(r);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        panelNamesList = new javax.swing.JPanel();
        inputNameRow3 = new wekimini.gui.IONameRow();
        inputNameRow4 = new wekimini.gui.IONameRow();
        inputNameRow2 = new wekimini.gui.IONameRow();
        inputNameRow1 = new wekimini.gui.IONameRow();
        buttonReset = new javax.swing.JButton();
        buttonDone = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Enter Input Names");
        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        panelNamesList.setLayout(new javax.swing.BoxLayout(panelNamesList, javax.swing.BoxLayout.Y_AXIS));
        panelNamesList.add(inputNameRow3);
        panelNamesList.add(inputNameRow4);
        panelNamesList.add(inputNameRow2);
        panelNamesList.add(inputNameRow1);

        jScrollPane1.setViewportView(panelNamesList);

        buttonReset.setText("Reset to default");
        buttonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetActionPerformed(evt);
            }
        });

        buttonDone.setText("Done");
        buttonDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDoneActionPerformed(evt);
            }
        });

        buttonCancel.setText("Cancel");
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(buttonReset)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonDone)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonReset)
                    .addComponent(buttonDone)
                    .addComponent(buttonCancel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed
        for (int i = 0; i < numNames; i++) {
            rows.get(i).setIOName(baseName + "-" + (i+1));
        }
    }//GEN-LAST:event_buttonResetActionPerformed

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        namesListReceiver.cancel();
        this.dispose();
    }//GEN-LAST:event_buttonCancelActionPerformed

    private void buttonDoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDoneActionPerformed
        String[] names = new String[numNames];
        for (int i = 0; i < rows.size(); i++) {
            names[i] = rows.get(i).getIOName();
        }
        if (!Util.checkAllUnique(names)) {
            Util.showPrettyErrorPane(this, "Names must be unique");
            return;
        }
        namesListReceiver.setNames(names);
        this.dispose();
    }//GEN-LAST:event_buttonDoneActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GuiIONameCustomise.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GuiIONameCustomise.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GuiIONameCustomise.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GuiIONameCustomise.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new GUIInputNameCustomise().setVisible(true);
                String[] currNames = null;
                String baseName = "input 1";
                NamesListReceiver r = new NamesListReceiver() {

                    @Override
                    public void setNames(String[] names) {
                        System.out.println("HELLO!");
                    }

                    @Override
                    public void cancel() {
                    }
                };
                new GuiIONameCustomise(5, baseName, currNames, r, IOType.OUTPUT).setVisible(true);
                
            }
        });
    }
    
    public interface NamesListReceiver {
        void setNames(String[] names);
        void cancel();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonDone;
    private javax.swing.JButton buttonReset;
    private wekimini.gui.IONameRow inputNameRow1;
    private wekimini.gui.IONameRow inputNameRow2;
    private wekimini.gui.IONameRow inputNameRow3;
    private wekimini.gui.IONameRow inputNameRow4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelNamesList;
    // End of variables declaration//GEN-END:variables
}
